﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web
{
    public partial class ToolsUpload : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListHelper.InitDropList(this.ddlType, typeof(ITOMMS.Business.PubEnums.ToolsType));
                if (Request.QueryString["id"] != null)
                {
                    string strId = Request.QueryString["id"].ToString();
                    int id = 0;
                    if (int.TryParse(strId, out id))
                    {
                        dataBind(id);
                    }
                }
            }
        }

        private void dataBind(int id)
        {
            tbUpLoadFilesEntity file = new Entity.tbUpLoadFilesEntity();
            file = file.GetById(id);
            if (file != null)
            {
                ViewState["Entity"] = file;
                txtTitle.Text = file.FileTitle;
                ddlType.SelectedValue = file.ToolType.ToString();
                txtdesicription.Text = file.Remark;
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTitle.Text))
            {
                //MessageBoxHelper.PopUpMessage("标题不能为空");
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('标题不能为空。',300,180,'提示',null);</script>");
                return;
            }


            if (ddlType.SelectedValue == "-1")
            {
                // MessageBoxHelper.PopUpMessage("类别不能为空");
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('类别不能为空。',300,180,'提示',null);</script>");
                return;
            }

            if (!FileUpload1.HasFile)
            {
                // MessageBoxHelper.PopUpMessage("类别不能为空");
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('必须要选择上传的文件。',300,180,'提示',null);</script>");
                return;
            }


            try
            {
                tbUpLoadFilesEntity file = new tbUpLoadFilesEntity();
                if (ViewState["Entity"] != null && ViewState["Entity"] is tbUpLoadFilesEntity)
                {
                    file = ViewState["Entity"] as tbUpLoadFilesEntity;
                }

                file.FileTitle = txtTitle.Text;
                file.ToolType = int.Parse(ddlType.SelectedValue);
             
                if (FileUpload1.HasFile)
                {
                    file.OriginalFileName = FileUpload1.FileName;
                    file.NewFileName = SaveToServer(FileUpload1);
                    file.FileType = file.NewFileName.Substring(file.NewFileName.IndexOf('.'));
                }
                file.Remark = txtdesicription.Text;


                file.SaveOrUpdate();
                ViewState["Entity"] = file;

                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.succeedInfo('上传成功。',300,180,'提示',null);</script>");
            }
            catch (Exception ex)
            {
                log4netHelper.error("上传文件失败", ex);
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('上传失败。',300,180,'提示',null);</script>");
            }
        }


        private string SaveToServer(FileUpload imageUpload)
        {
            string rs = string.Empty;
            try
            {
                if (imageUpload.PostedFile.FileName == "")
                {
                    return rs;
                }
                string year = System.DateTime.Now.Year.ToString();
                string month = System.DateTime.Now.Month.ToString();
                string day = System.DateTime.Now.Day.ToString();
                string hour = System.DateTime.Now.Hour.ToString();
                string minute = System.DateTime.Now.Minute.ToString();
                string second = System.DateTime.Now.Second.ToString();
                string fileName = year + month + day + hour + minute + second;


                string filePath = imageUpload.PostedFile.FileName;
                string fileEx = filePath.Substring(filePath.LastIndexOf(".") + 1);

                if (!System.IO.Directory.Exists(Server.MapPath(@"~/UpLoadFiles\")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(@"~/UpLoadFiles\"));
                }


                string newfilename = Server.MapPath(@"~/UpLoadFiles\") + fileName + "." + fileEx;
                imageUpload.SaveAs(newfilename);
                rs = fileName + "." + fileEx;


            }
            catch
            {
                MessageBoxHelper.PopUpMessage("文件上传失败");
            }
            return rs;
        }

    }
}
